package com.digital.smartgrow.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.common.util.MD5Util;
import com.digital.smartgrow.dto.LoginDTO;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.entity.expert.Expert;
import com.digital.smartgrow.service.datacenter.TeacherService;
import com.digital.smartgrow.service.expert.ExpertService;
import com.github.benmanes.caffeine.cache.Cache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author xiewb
 * @since 2022-05-08
 */
@Api(tags = "登录")
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private Cache<String, Teacher> teacherCache;
    @Autowired
    private ExpertService expertService;

    //@ApiOperation(value = "教师登录")
    //@PostMapping("/teacherLogin")
    public Object teacherLogin_bak(@RequestBody LoginDTO loginDTO) {
        System.out.println("loginDTO = " + loginDTO);
        if (StringUtils.isBlank(loginDTO.getUserName())) return Result.fail("账号不能为空");
        if (StringUtils.isBlank(loginDTO.getUserPwd())) return Result.fail("密码不能为空");
        Teacher teacher = teacherService.getInfoByUserName(loginDTO.getUserName());
        if (teacher == null) return Result.fail("账号不存在");
        String password = MD5Util.MD5(loginDTO.getUserPwd() + teacher.getSalt());
        if (StringUtils.isBlank(password) || !password.equals(teacher.getPassword()))
            return Result.fail("密码错误");
        return teacherService.login(teacher);
    }

    @ApiOperation(value = "教师登录")
    @PostMapping("/teacherLogin")
    public Object teacherLogin(@RequestBody LoginDTO loginDTO) {
        System.out.println("loginDTO = " + loginDTO);
        if (StringUtils.isBlank(loginDTO.getUserName())) return Result.fail("账号不能为空");
        if (StringUtils.isBlank(loginDTO.getUserPwd())) return Result.fail("密码不能为空");

        Teacher teacher = teacherService.getInfoByUserName(loginDTO.getUserName());
        Expert expert = expertService.getInfoByUserName(loginDTO.getUserName());
        if (teacher == null&&expert==null) return Result.fail("账号不存在");
        String password="";
        Teacher login=null;
        if(teacher!=null){
            password  = MD5Util.MD5(loginDTO.getUserPwd() + teacher.getSalt());
            if (StringUtils.isBlank(password) || !password.equals(teacher.getPassword()))
                return Result.fail("密码错误");
             login = teacherService.login(teacher);
        }
        if(expert!=null){
            password  = loginDTO.getUserPwd() ;
            if (StringUtils.isBlank(password) || !password.equals(expert.getPassword()))
                return Result.fail("密码错误");
             login = teacherService.login(expert);
        }

        return login;

    }
    @ApiOperation(value = "专家登录")
    @PostMapping("/expertLogin")
    public Object expertLogin(@RequestBody LoginDTO loginDTO) {
        System.out.println("loginDTO = " + loginDTO);
        if (StringUtils.isBlank(loginDTO.getUserName())) return Result.fail("账号不能为空");
        if (StringUtils.isBlank(loginDTO.getUserPwd())) return Result.fail("密码不能为空");
        Expert expert = expertService.getInfoByUserName(loginDTO.getUserName());
        //Teacher teacher = teacherService.getInfoByUserName(loginDTO.getUserName());
        if (expert == null) return Result.fail("账号不存在");
        //String password = MD5Util.MD5(loginDTO.getUserPwd() + teacher.getSalt());
        if ( !loginDTO.getUserPwd().equals(expert.getPassword()))
            return Result.fail("密码错误");
        //权限在改造，都返回Teacher用户
        return teacherService.login(expert);
    }

    @ApiOperation(value = "教师登出")
    @GetMapping("/teacherLogout")
    public void teacherLogin(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (token == null) {
            token = request.getHeader("token");//header方式
        }
        if (StringUtils.isNotBlank(token))
            teacherCache.asMap().remove(token);
    }
}
